Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

464
Visualizações
Mongoose query not running - "cursor.toArray is not a function"

MongoDB beginner, having trouble getting queries to work. Was following a tutorial of sorts and it was a demo notes app. Their syntax for saving new notes works fine.

However when it comes to printing out the list of notes, there seems to be something wrong in the syntax given to me or something im doing wrong.

const mongoose = require("mongoose");

const url =
    "mongodb+srv://Saif:<password>@cluster0.8d2lb.mongodb.net/notes-app?retryWrites=true&w=majority";

mongoose.connect(url, {
    useNewUrlParser: true,
});

const noteSchema = new mongoose.Schema({
    content: String,
    date: Date,
    important: Boolean,
});

const Note = mongoose.model("Note", noteSchema);

Note.find({}).then((result) => {
    result.forEach((note) => {
        console.log(note);
    });
    mongoose.connection.close();
});

After looking up documentation, the actual syntax of find is a little different where they pass in a callback instead of using promises. But changing that block to use a callback still doesnt work

Note.find({}, (error, data) => {
    if (error) {
        console.log(error);
        
    } else {
        data.forEach((note) => {
            console.log(note);
        })   
    }
    mongoose.connection.close()

})

Error

TypeError: cursor.toArray is not a function
    at model.Query.<anonymous> (D:\Folders\Documents\CS.........

(Use `node --trace-warnings ...` to show where the warning was created)
(node:27108) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:27108) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

.find method from model returns Query object not Cursor

For cursor you need to do .exec

Note.find({}).exec((error, data) => {
    if (error) {
        console.log(error);
        
    } else {
        data.forEach((note) => {
            console.log(note);
        })   
    }
    mongoose.connection.close()

})
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda